package com.hefei.hight.reflection;

import com.hefei.hight.annotation.AgeValidator;

import java.lang.reflect.Method;

/**
 * @author caizhengjun
 * @date 2020/9/5 1:52 下午
 */

public class Test11 {
    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("com.hefei.hight.reflection.Student");
        Object o = aClass.newInstance();

        Method method = aClass.getDeclaredMethod("setAge", Integer.class);

        int val = 6;

        AgeValidator annotation = method.getAnnotation(AgeValidator.class);

        int min = annotation.min();
        int max = annotation.max();

        if (val < min || val > max) {
            throw new RuntimeException("年龄非法");
        }
        method.invoke(o, 20);
    }
}
